(function(){
    function GetMachineFlow(){
        this.init();
  
     
       
    }

    GetMachineFlow.prototype = {
        constructor:GetMachineFlow,
        init:function(){
            this.nodata = '<tr><td  colspan="6">暂无信息</td></tr>';
            var _this = this;
        
              $.datetimepicker.setLocale('ch');
				$('#endtime').datetimepicker({
                    format: 'Y-m-d',
                    value:new Date().dateadd("d", 1),
					maxDate: new Date(),
					onChangeDateTime:function(dp,$input){
						var limitStart = new Date($('#begintime').val().replace('-','/'));
						var nowStart = new Date($('#endtime').val().replace('-','/'));
						if(limitStart > nowStart){
							$('#endtime').val(limitStart.format("YY-M-D"))

						}
					},
					timepicker: false
                });
                
                $('#begintime').datetimepicker({
                    format: 'Y-m-d',
                    value:new Date().dateadd("d", -1),
                    maxDate: new Date(),
					onChangeDateTime:function(dp,$input){
                        var limitStart = new Date($('#endtime').val().replace('-','/'));
						var nowStart = new Date($('#begintime').val().replace('-','/'));
						if(limitStart < nowStart){
							$('#begintime').val(limitStart.format("YY-M-D"))

						}
						
					},
					timepicker: false
                });
                

                this.getMachineList(1);

                $.extend({page:function(pn){
                    _this.getMachineList(pn);
                }});
            
                $("#searchBtn").unbind("click").click(function(){
                     _this.getMachineList(1);
                });

          




        },
        getMachineList:function(pn){
            $("#countDatas").html("");
            var _this = this;
            var sid = location.search.getParam("sid");
            var flowType = $("#transType").val();
            if( flowType )
            {
                flowType = "&type="+flowType;
            }
            var stime = $("#begintime").val()  ;
            var etime = $("#endtime").val() ;

            // 导出明细
            var downLink = '/sys?fid=312&siteid='+sid+flowType+'&stime='+stime+' 00:00:00&etime='+etime+' 23:59:59';
            $("#downDetails").attr("href", downLink);
            $.ajax({
                type:"get",
                url:"/sys?fid=309&siteid="+sid+flowType+"&stime="+stime+" 00:00:00"+"&etime="+etime+" 23:59:59"+"&ps=15&pn="+pn,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        // type  0.扣款、1.出票、2.兑奖 3. 加款
                        var stateArr = ["扣款","出票","兑奖", "加款"];
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var siteid  = value.siteid ;
                                var ticketid = value.ticketid ;
                                var type = value.type;
                                var sysType = stateArr[value.type];
                                var sysmoney = value.sysmoney.toFixed(2);//进出账
                                var createtime = value.createtime;
                                var markClass = '';
                                if( type == 0 || type == 1 )
                                {
                                    markClass = 'marksysmoney1';
                                    sysmoney = "-"+sysmoney;
                                }
                                else
                                {
                                    markClass = 'marksysmoney';
                                    sysmoney = "+"+sysmoney; 
                                }

                              
                               

                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                <td>'+siteid+'</td>\
                                                <td>'+ticketid+'</td>\
                                                <td>'+sysType+'</td>\
                                                <td  class="'+markClass+'">'+sysmoney+'</td>\
                                                <td  ></td>\
                                                <td>'+createtime+'</td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#machineFlow").html( strArr.join(""));

                            

                            // 数据汇总
                             _this.getDataCount();

                             // 分页
                             var pn = count.pn;
                             var tp = count.tp;
                             var tr = count.tr;
                             $("#countpages").html( tp );
                             $("#countnums").html( tr );
                             $("#totalpages").html( tr );
         
                             var pageHtml = getPages(pn,tp, "$.page");
 
                             $("#paginationSel").html(pageHtml);
                          

                        }
                        else
                        {
                            $("#machineFlow").html(_this.nodata);
                            $("#paginationSel").html("");
                            $("#countpages").html( 0 );
                            $("#countnums").html( 0 );

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        // 数据汇总
        getDataCount:function(){
            var sid = location.search.getParam("sid");
            var flowType = $("#transType").val();
            var stateArr = ["扣款","出票","兑奖", "加款"];
            var statusDesc = '进出账';
            if( flowType )
            {
                flowType = "&type="+flowType;
                statusDesc = stateArr[ parseInt( $("#transType").val() ) ];
            }
            var stime = $("#begintime").val() + " 00:00:00" ;
            var etime = $("#endtime").val() + " 23:59:59" ;
            $.ajax({
                type:"get",
                url:"/sys?fid=311&siteid="+sid+flowType+"&stime="+stime+"&etime="+etime,
                dataType:"json",
                success:function( info ){
                    var data = info.data;
                    var sMoney = Math.abs(parseFloat(data.sysmoney)).toFixed(2);
                    $("#countDatas").html( statusDesc+':  <i>'+sMoney+'</i> 元   '  );

                }

            });
        }
 
      
      
    };





  new GetMachineFlow();

}());